package edu.study.common.util;

import org.apache.commons.codec.digest.HmacUtils;
import org.springframework.util.DigestUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordUtil {

    public static String getMd5Encode(String passWord) {
        return DigestUtils.md5DigestAsHex(passWord.getBytes());
    }

    public static boolean comparePwd(String passWord, String md5Str) {
        return DigestUtils.md5DigestAsHex(passWord.getBytes()).equals(md5Str);
    }

    public static void main(String[] args) {
        // 用户密码加解密
        // Abc123 ---> 0c93f35d5e0ff177061fcce83e043c2f
        // Abc1234 ---> b6ebdb91d7c1759e7eaad95c87e7bfbd
        String hmacMd5Pwd = HmacUtils.hmacMd5Hex("ZHONGAN_CHAIN", "Abc1234");
        String password = PasswordUtil.getMd5Encode("bsy@2019" + hmacMd5Pwd);
        System.out.println(hmacMd5Pwd);
        System.out.println(password);

        //数据库字段加解密
        /*SensitiveCipher sensitiveCipher = SensitiveCipher.getInstance();
        String after = "1DDC0D2B55EE8267C018A615F18B4087EAC5A1DAACC45E272B111EE4056FD5D9";
        String expected = sensitiveCipher.sensitiveAESDecrypt(after);
        String expectdAfter = sensitiveCipher.sensitiveAESEncrypt(expected);
        boolean equals = after.equals(expectdAfter);
        System.out.println(expected);
        System.out.println(equals);*/


        String a = "${dd},${b},##aa##";
        System.out.println(a.indexOf("${"));
        a = a.replaceAll(Pattern.quote("##aa##"), Matcher.quoteReplacement("}$#"));
        System.out.println(a);
        System.out.println(Pattern.quote("${dd}"));
        System.out.println(Matcher.quoteReplacement("https://"));


        String s="selecttop${fetchNum}*fromtbl";
        String escaped=Pattern.quote("${fetchNum}");//=>"\Q${fetchNum}\E"
        String replaced=s.replaceAll(escaped,"123");
        System.out.println(replaced);

    }
}